import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ViewPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ViewPageState();
}

class _ViewPageState extends State<ViewPage> {
  var imgList = [
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image1.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image2.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image3.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image4.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image5.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image6.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image7.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image8.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image9.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon/coming_soon_image10.jpg',
  ];
  late PageController _pageController;

  var _currPageValue = 0.0;

  int _currentPageIndex = 0;

  //缩放系数
  double _scaleFactor = .8;

  //view page height
  double _height = 230.0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(viewportFraction: 0.9);
    _pageController.addListener(() {
      setState(() {
        _currPageValue = _pageController.page!;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          height: _height,
          child: PageView.builder(
            itemBuilder: (context, index) => _buildPageItem(index),
            itemCount: imgList.length,
            controller: _pageController,
            onPageChanged: (int index) {
              setState(() {
                _currentPageIndex = index % (imgList.length);
              });
            },
          ),
        ),
        Positioned(bottom: 10, left: 0, right: 0, child: _idicator()),
      ],
    );
  }

  _idicator() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(imgList.length, (i) {
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 5),
          width: 10,
          height: 10,
          decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: _currentPageIndex == i ? Colors.blue : Colors.grey),
        );
      }).toList(),
    );
  }

  _buildPageItem(int index) {
    Matrix4 matrix4 = Matrix4.identity();
    if (index == _currPageValue.floor()) {
      //当前的item
      var currScale = 1 - (_currPageValue - index) * (1 - _scaleFactor);
      var currTrans = _height * (1 - currScale) / 2;

      matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
        ..setTranslationRaw(0.0, currTrans, 0.0);
    } else if (index == _currPageValue.floor() + 1) {
      //右边的item
      var currScale =
          _scaleFactor + (_currPageValue - index + 1) * (1 - _scaleFactor);
      var currTrans = _height * (1 - currScale) / 2;

      matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
        ..setTranslationRaw(0.0, currTrans, 0.0);
    } else if (index == _currPageValue.floor() - 1) {
      //左边
      var currScale = 1 - (_currPageValue - index) * (1 - _scaleFactor);
      var currTrans = _height * (1 - currScale) / 2;

      matrix4 = Matrix4.diagonal3Values(1.0, currScale, 1.0)
        ..setTranslationRaw(0.0, currTrans, 0.0);
    } else {
      //其他，不在屏幕显示的item
      matrix4 = Matrix4.diagonal3Values(1.0, _scaleFactor, 1.0)
        ..setTranslationRaw(0.0, _height * (1 - _scaleFactor) / 2, 0.0);
    }

    return Transform(
      transform: matrix4,
      // transform: Matrix4.identity(),

      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 10.w),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(10.w),
          child: Image.network(
            imgList[index % 10],
            fit: BoxFit.fill,
            frameBuilder: (BuildContext context, Widget child, int? frame,
                bool wasSynchronouslyLoaded) {
              if (frame == null) {
                return Image.asset(
                  'assets/images/dog.jpg',
                  height: 150,
                  width: 150,
                  fit: BoxFit.cover,
                );
              }
              return AnimatedContainer(
                duration: Duration(microseconds: 500),
                child: child,
              );
            },
          ),
        ),
      ),
    );
  }
}
